﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AISFMIOperator
{
    public enum Action { add, edit, delete, view }
    public partial class InsuranceProgramForm : Form
    {
        private Action action = Action.view;
        private InsuranceProgram ins_program = new InsuranceProgram();

        public InsuranceProgramForm()
        {
            InitializeComponent();
            facilityTable.AutoGenerateColumns = false;
            koifTable.AutoGenerateColumns = false;
            serviceTable.AutoGenerateColumns = false;

            this.ins_program.InsuranceProgramID = 0;
            this.ins_program.MedicalServices = new List<MedicalService>();
            this.ins_program.Facilities = new List<Facility>();
            this.ins_program.Coifficients = new List<InsuranceProgramCoefficient>();

            FullInsProgrammsList();

            //action = Action.view;

            //RefreshAllData();
        }

        private void FullInsProgrammsList()
        {
            this.insprogramcomboBox.DataSource = InsuranceProgram.GetAllInsurancePrograms();
            this.insprogramcomboBox.DisplayMember = "InsuranceProgramName";
        }

        public void RefreshAllData()
        {
            if (action == Action.view)
            {
                RefreshOnActionView();
            }
            else if (action == Action.add)
            {
                RefreshOnAddAction();
            }
            else if (action == Action.edit)
            {
                RefreshOnActionEdit();
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void InsuranceProgramForm_Load(object sender, EventArgs e)
        {

        }

        private void addinsprogrambutton_Click(object sender, EventArgs e)
        {
            action = Action.add;
            RefreshAllData();
        }

        private void RefreshOnAddAction()
        {
            age_from_tb.Enabled = true;
            age_to_tb.Enabled = true;
            coif_tb.Enabled = true;

            insprogramcomboBox.Enabled = false;
            editinsprogrambutton.Enabled = false;
            deleteinsprogrambutton.Enabled = false;
            addinsprogrambutton.Enabled = false;

            ins_progrm_name_tb.Enabled = true;
            ins_progrm_name_tb.Text = "";

            inssum.Enabled = true;
            inssum.Text = "";

            inspremium.Enabled = true;
            inspremium.Text = "";

            koifTable.DataSource = null;
            serviceTable.DataSource = null;
            facilityTable.DataSource = null;

            koifTable.Enabled = true;
            serviceTable.Enabled = true;
            facilityTable.Enabled = true;

            addkoifbutton.Enabled = true;
            deletekoifbutton.Enabled = true;

            addfacilitycomboBox.Enabled = true;
            addfacilitybutton.Enabled = true;
            deletefacilitybutton.Enabled = true;

            addservicebutton.Enabled = true;
            addservicecomboBox.Enabled = true;
            deleteservicebutton.Enabled = true;

            savebutton.Enabled = true;
            cancelbutton1.Enabled = true;

            addfacilitycomboBox.DataSource = Facility.GetAllFacilities();
            addfacilitycomboBox.DisplayMember = "FacilityName";

            addservicecomboBox.DataSource = MedicalService.GetAllMedicalServices();
            addservicecomboBox.DisplayMember = "MedicalServiceName";

        }

        private void RefreshOnActionEdit()
        {
            ins_progrm_name_tb.Enabled = true;
            inssum.Enabled = true;
            inspremium.Enabled = true;

            koifTable.Enabled = true;
            serviceTable.Enabled = true;
            facilityTable.Enabled = true;

            addkoifbutton.Enabled = true;
            deletekoifbutton.Enabled = true;

            addfacilitycomboBox.Enabled = true;
            addfacilitybutton.Enabled = true;
            deletefacilitybutton.Enabled = true;

            addservicebutton.Enabled = true;
            addservicecomboBox.Enabled = true;
            deleteservicebutton.Enabled = true;

            savebutton.Enabled = true;
            cancelbutton1.Enabled = true;
        }

        private void RefreshOnActionView()
        {
            if (ins_program != null)
            {
                this.ins_progrm_name_tb.Text = ins_program.InsuranceProgramName;
                this.inspremium.Text = ins_program.InsuranceProgramPremium.ToString();
                this.inssum.Text = ins_program.InsuranceProgramSumm.ToString();

                FullFacilitiesTableFromInsProgram();
                FullServicesTableFromInsProgram();
                FullCoiffsTableFromInsProgram();
            }
        }

        private void cancelbutton1_Click(object sender, EventArgs e)
        {

        }

        private void savebutton_Click(object sender, EventArgs e)
        {
            if (action == Action.add)
            {
                ins_program.InsuranceProgramID = 0;
                ins_program.InsuranceProgramName = ins_progrm_name_tb.Text.Trim();
                ins_program.InsuranceProgramPremium = float.Parse(inspremium.Text.Trim());
                ins_program.InsuranceProgramSumm = float.Parse(inssum.Text.Trim());

                ins_program.Save();
            }
            else if (action == Action.edit)
            {

            }
        }

        private void addfacilitybutton_Click(object sender, EventArgs e)
        {
            Facility facility = (Facility)addfacilitycomboBox.SelectedItem;
            this.ins_program.Facilities.Add(facility);
            FullFacilitiesTableFromInsProgram();
        }

        private void FullFacilitiesTableFromInsProgram()
        {
            this.facilityTable.DataSource = null;
            this.facilityTable.DataSource = this.ins_program.Facilities;
            this.facilityTable.Columns[0].DataPropertyName = "FacilityName";
            this.facilityTable.Columns[1].DataPropertyName = "FullAddress";
        }

        private void addservicebutton_Click(object sender, EventArgs e)
        {
            MedicalService service = (MedicalService)addservicecomboBox.SelectedItem;
            this.ins_program.MedicalServices.Add(service);
            FullServicesTableFromInsProgram();
        }

        private void FullServicesTableFromInsProgram()
        {
            this.serviceTable.DataSource = null;
            this.serviceTable.DataSource = this.ins_program.MedicalServices;
            serviceTable.Columns[0].DataPropertyName = "MedicalServiceName";
            serviceTable.Columns[1].DataPropertyName = "Cost";
        }

        private void addkoifbutton_Click(object sender, EventArgs e)
        {
            InsuranceProgramCoefficient ipc = new InsuranceProgramCoefficient();
            ipc.InsuranceProgramID = 0;
            ipc.AgeFrom = int.Parse(age_from_tb.Text.Trim());
            ipc.AgeTo = (age_to_tb.Text.Trim() == "") ? 0 : (int)(int.Parse(age_to_tb.Text.Trim()));
            ipc.Coifficient = float.Parse(coif_tb.Text.Trim());
            ins_program.Coifficients.Add(ipc);

            FullCoiffsTableFromInsProgram();
        }
        private void FullCoiffsTableFromInsProgram()
        {
            koifTable.DataSource = null;
            koifTable.DataSource = ins_program.Coifficients;
            koifTable.Columns[0].DataPropertyName = "AgeFrom";
            koifTable.Columns[1].DataPropertyName = "AgeTo";
            koifTable.Columns[2].DataPropertyName = "Coifficient";
        }

        private void insprogramcomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            action = Action.view;
            if (insprogramcomboBox.Items.Count > 0)
            {
                this.ins_program = (InsuranceProgram)insprogramcomboBox.Items[0];
            }
            RefreshAllData();
        }
    }
}